package xx.services.management.rest;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import xx.db.sql.Update;
import xx.entity.manage.Category;
import xx.entity.manage.CategoryLink;
import xx.services.Passport;
import xx.services.management.inter.ICategoryInterface;
import xx.services.util.Constants;

/**
 * 后台管理分类服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/management/category")
 * @author Administrator 2010-12-19
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/management/category")
public class CagegoryRest extends Passport implements ICategoryInterface
{

    public CagegoryRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 在分类{category_id}下增加子分组，
     * 
     * @POST
     * @Path("/{category_id}")
     * @param categoryId-@PathParam("category_id")
     *            分组ID
     * @param name-@FormParam("name")
     *            分组名称
     * @param pageId-@FormParam("page_id")
     *            与分组关联的页面ID
     * @param sortId-@FormParam("sort_category_id")
     *            兄弟节点ID（>0时，表示把新增的分组排在categoryId的后台，<0时，表示把新增的分组排在categoryId的前边，=0时，表示排在最后边）
     * @param sign-@FormParam("sign")
     *            分类标识
     * @param description-@FormParam("description")
     *            分类描述
     * @return
     */
    @POST
    @Path("/{category_id}")
    public String add(@PathParam("category_id")
    long categoryId, @FormParam("name")
    String name, @FormParam("page_id")
    long pageId, @FormParam("sort_category_id")
    long sortId, @FormParam("sign")
    int sign, @FormParam("description")
    String description)
    {
        try
        {
            Category c = new Category(qr, owner);
            long categoryid = c.setSign(sign).setName(name).setParentId(categoryId).setDescription(
                    description).insert();
            if (pageId > 0)
            {
                CategoryLink cl = new CategoryLink(qr, owner);
                cl.setCategoryId(categoryid).setPageId(pageId).insert();
            }

            if (sortId > 0)
            {
                c.sortAfter(sortId);
            }
            else if (sortId < 0)
            {
                c.sortBefore(sortId * (-1));
            }

            return ok(c);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 删除分类
     * 
     * @DELETE
     * @Path ("/{category_id}")
     * @param categoryId-@FormParam("category_id")
     *            分类ID
     * @return
     */
    @DELETE
    @Path("/{category_id}")
    public String delete(@FormParam("category_id")
    long categoryId)
    {
        try
        {
            Category c = new Category(qr, owner);
            c.setCategoryId(categoryId).delete();

            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取第一级分组,当categoryId为0时，获取根分级
     * 
     * @GET
     * @Path ("/{category_id}/children")
     * @param categoryId-@PathParam("category_id")
     *            分类ID
     * @param jsonkeys-@QueryParam("jsonkeys")
     *            相关JSONkey
     * @return 分类列表
     */
    @GET
    @Path("/{category_id}/children")
    public String getChildren(@PathParam("category_id")
    long categoryId, @QueryParam("jsonkeys")
    String jsonkeys)
    {
        try
        {
            Category c = new Category(qr, owner);
            c.setCategoryId(categoryId).addJsonKey(jsonkeys);
            return ok(c.getChildrenEx());
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取指定分组信息
     * 
     * @GET
     * @Path ("/{category_id}")
     * @param categoryId-@PathParam
     *            ("category_id") 分类ID
     * @return 分类实体
     */
    @GET
    @Path("/{category_id}")
    public String getInfo(@PathParam("category_id")
    long categoryId)
    {
        try
        {
            Category c = new Category(qr, owner);
            c.setCategoryId(categoryId).load();
            return ok(c);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 修改分类
     * 
     * @PUT
     * @Path ("/{category_id}")
     * @param categoryId-@PathParam("category_id")
     *            分类ID
     * @param name-@FormParam("name")
     *            分类名称
     * @param pageId-@FormParam("page_id")
     *            关联page页面ID
     * @param description-@FormParam("description")
     *            分类描述
     * @return 新的分类实体
     */
    @PUT
    @Path("/{category_id}")
    public String modify(@PathParam("category_id")
    long categoryId, @FormParam("name")
    String name, @FormParam("page_id")
    long pageId, @FormParam("description")
    String description)
    {
        try
        {
            Category c = new Category(qr, owner);
            c.setCategoryId(categoryId).setName(name).setDescription(description).update(
                    Category.NAME, Category.DESCRIPTION);
            if (pageId > 0)
            {
                Update u = new Update(qr, owner);
                u.update(CategoryLink.class).set(CategoryLink.PAGE_ID, pageId).where(
                        CategoryLink.CATEGORY_ID + "=" + categoryId).go();
            }
            return ok(c);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }
}
